package cn.jinjimi.app.main.dialog;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import cn.jinjimi.app.R;
import cn.jinjimi.app.poster.bean.PosterInfo;
import cn.jinjimi.app.poster.interfaces.IPosterLoadMore;
import cn.jinjimi.app.poster.interfaces.PosterDialogView;

/**
 * Created by lforxeverc on 2016/11/21.
 */

public class SharePosterDialog extends DialogFragment implements PosterDialogView {
    SharePosterFragment mFragment;
    FragmentActivity mContext;
    IPosterLoadMore mListener;
    int position, total, categoryCode;

    private String source;

    public SharePosterDialog() {
    }

    public void setContext(FragmentActivity mContext) {
        this.mContext = mContext;
    }

    public void setListener(IPosterLoadMore mListener) {
        this.mListener = mListener;
    }

    public void setCategoryCode(int categoryCode) {
        this.categoryCode = categoryCode;
    }

    public void setSource(String source) {
        this.source = source;
    }

    public SharePosterDialog setInfos(int position, int total) {
        this.position = position;
        this.total = total;
        return this;
    }

    public void onLoadPositionDataSuccess(int position, PosterInfo info, boolean refresh) {
        mFragment.onLoadPositionDataSuccess(position, info, refresh);
    }

    public void onLoadPositionDataFail(int position) {
        mFragment.onLoadPositionDataFail(position);
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(DialogFragment.STYLE_NORMAL, android.R.style.Theme_Translucent_NoTitleBar);

    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mFragment = new SharePosterFragment();
        mFragment.setContext(mContext);
        mFragment.setLoadMoreListener(mListener);
        mFragment.setInfos(position, total);
        mFragment.setPosterDialogView(this);
        mFragment.setCategoryCode(categoryCode);
        mFragment.setSource(source);
        View view = inflater.inflate(R.layout.dialog_poster_list, container, false);
        FragmentManager fm = getChildFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        ft.replace(R.id.container, mFragment);
        ft.commitAllowingStateLoss();
        return view;
    }

    @Override
    public void dismissDialog() {
        if (getActivity() != null)
            dismissAllowingStateLoss();
    }

}
